package cn.zhangmh.easymvp.books.ui;

import android.content.Context;
import android.os.Bundle;
import android.app.Fragment;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import cn.zhangmh.easymvp.R;
import cn.zhangmh.easymvp.books.entity.BookBean;
import cn.zhangmh.easymvp.books.entity.Books;

/**
 * A fragment representing a list of Items.
 * <p/>
 * Activities containing this fragment MUST implement the {@link OnListFragmentInteractionListener}
 * interface.
 */
public class BookBeanFragment extends Fragment {

    // TODO: Customize parameter argument names
    private static final String ARG_COLUMN_COUNT = "column-count";
    private  static  final  String DEBUG = "调试输出信息---->";

    private int mColumnCount = 1;
    private OnListFragmentInteractionListener mListener;
    private MyBookBeanRecyclerViewAdapter adapter;
    private  XRecyclerView recyclerView;
    private static Books books;
    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public BookBeanFragment() {
    }

    /**
     * 此处传入的columnCount决定了后续展现的样式，如果是list就显式限制为1,或者说应该修改实例方法为不传这个参数
     * @param --columnCount 这里默认参数是个坑，找了一天才发现就是多余的一个参数传错了！！！！！！！！！！
     * @param mBooks
     * @return
     */
    public static BookBeanFragment newInstance(Books mBooks) {
        BookBeanFragment fragment = new BookBeanFragment();
        books = mBooks;
        return fragment;
    }

    public  void  updateBooks(Books updateBooks){
        books = updateBooks;
        adapter.updateMvalues(books.getBooks());
        adapter.notifyDataSetChanged();
        recyclerView.refreshComplete();
        recyclerView.loadMoreComplete();
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_bookbean_list, container, false);

        Log.d("绘制view", "onCreateView: ");
        // Set the adapter
        if (view instanceof RecyclerView) {
            Context context = view.getContext();
            recyclerView = (XRecyclerView) view;
            LinearLayoutManager layoutManager = new LinearLayoutManager(context);
            recyclerView.setLayoutManager(layoutManager);
            recyclerView.setRefreshProgressStyle(ProgressStyle.BallClipRotate);
            recyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
            recyclerView.setLoadingMoreEnabled(true);
             adapter = new MyBookBeanRecyclerViewAdapter(books.getBooks(), mListener);
            recyclerView.setLoadingListener(new XRecyclerView.LoadingListener(){
                @Override
                public void onRefresh() {
                    mListener.onRefreashPull(books.getBooks().size());

                    Log.d(DEBUG, "onRefresh: ");
                }

                @Override
                public void onLoadMore() {
                    mListener.onLoadingMorePull(books.getBooks().size());
                    Log.d(DEBUG, "onLoadMore: ");
                }
            });
            recyclerView.setAdapter(adapter);
        }
        return view;
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnListFragmentInteractionListener) {
            mListener = (OnListFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnListFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnListFragmentInteractionListener {
        void onListFragmentInteraction(BookBean item);
        void  onRefreashPull(@NonNull int total);
        void  onLoadingMorePull(@NonNull int total);
    }

    /**
     * 由activity显式调用
     * @param mListener
     */
    public  void  setOnListFragmentInteractionListener(OnListFragmentInteractionListener mListener  ){
        this.mListener = mListener;
    }

}
